package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/users")
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private IStrategyService strategyService;


    @RequireLogin
    @GetMapping("/list")
    public Object list(){
        return userInfoService.listAll();
    }

    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){
        boolean ret = userInfoService.checkPhone(phone);
        return !ret;
    }

    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone);
        return JsonResult.success();
    }

    /**
     *
     * @param phone
     * @param nickname
     * @param password
     * @param rpassword
     * @param verifyCode
     * @return
     */
    @PostMapping("/regist")
    public Object regist(String phone,String nickname,String password,String rpassword,String verifyCode){
        /*try {
            userInfoService.regist(phone, nickname, password, rpassword, verifyCode);
        }catch (LogicException ex) {
            // 暴露给用户看的异常
            ex.printStackTrace();
            return JsonResult.error(JsonResult.CODE_ERROR_PARAM,ex.getMessage(),null);
        }catch (Exception e){
            // 系统异常
            e.printStackTrace();
            // 此处用法无法区分是系统异常还是能暴露给用户看的异常，
//            return JsonResult.error(JsonResult.CODE_ERROR_PARAM,e.getMessage(),null);
            return JsonResult.defaultError();

        }*/
        userInfoService.regist(phone, nickname, password, rpassword, verifyCode);
        return JsonResult.success();
    }

    @PostMapping("/login")
    public Object login(String username,String password){
        // 登录效验
        UserInfo user = userInfoService.login(username,password);
        // 将用户存到redis中
        String token = userInfoRedisService.setToken(user);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",user);

        return JsonResult.success(map);
    }

    @RequireLogin
    @GetMapping("/currentUser")
    public Object currentUser(HttpServletRequest request){
        // 获取请求头token
        String token = request.getHeader("token");
        // 通过token获取user对象
        UserInfo userInfo = userInfoRedisService.findUserInfoByToken(token);

        return JsonResult.success(userInfo);
    }

    @GetMapping("/users")
    public Object users(UserInfo userInfo){
        return JsonResult.success(userInfo);
    }

    /**
     * 查询用户id收藏攻略集合
     * @param userId
     * @return
     */
    @RequireLogin
    @GetMapping("/strategies/favor")
    public Object favor(String userId){
        List<String> list = userInfoRedisService.queryByStrategyFavorList(userId);
        return JsonResult.success(list);
    }
}
